from base.request_new import RequestNew
from business.ssologin.login import user_login
import asyncio
from business.mozi.template.mozi_template import *
import sys
from base.logcontrol import logger


class ProductGroupInfo:

    def __init__(self, product_group_id=None, outline_id=None):
        self.r = RequestNew(cookie=user_login())
        self._product_group_id = product_group_id
        self._outline_id = outline_id

    @property
    def product_group_id(self):
        return self._product_group_id

    @product_group_id.setter
    def product_group_id(self, value):
        self._product_group_id = value

    @property
    def outline_id(self):
        return self._outline_id

    @outline_id.setter
    def outline_id(self, value):
        if isinstance(value, str):
            self._outline_id = value

    def get_product_group_info(self):
        try:
            r, v = self.r.request_auto(**get_product_group_info_conf(self.product_group_id))
        except RuntimeError:
            logger.error(f"获取商品信息失败，请检查商品包id:{self.product_group_id}")
            sys.exit()
        return v

    def get_outline_info(self, outline_id):
        try:
            r, v = self.r.request_auto(**get_outline_info_conf(outline_id))
        except RuntimeError:
            logger.error(f"获取大纲信息失败，请检查大纲id:{outline_id}")
            sys.exit()
        return v

    def get_template_period_info(self, template_period_id):
        r, v = self.r.request_auto(**get_template_period_info_conf(template_period_id))
        return v

    def run(self):
        r = dict()
        outline_id = self.outline_id
        if not outline_id:
            return
        if self.product_group_id:
            product_group_info = self.get_product_group_info()
            r.update({"商品包": product_group_info})
            outline_id = product_group_info["outline_id"]
        if not outline_id:
            return "大纲id为空"
        outline_info = self.get_outline_info(outline_id)
        r.update({"大纲": outline_info})
        template_period_id = outline_info["template_period_id"]
        template_periodical_list = self.get_template_period_info(template_period_id)
        r.update({"校历": template_periodical_list})
        return r


if __name__ == '__main__':
    p = ProductGroupInfo()
    from pprint import pprint

    p.product_group_id = ""
    pprint(p.run())
